package com.ocom.payhandle.controller;

import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.PayHandleUrl;
import com.ocom.common.entity.card.EntityXfDeviceInfo;
import com.ocom.common.entity.card.EntityXfMerchantInfo;
import com.ocom.common.entity.card.EntityXfPointInfo;
import com.ocom.common.entity.card.EntityXfSignNo;
import com.ocom.common.entity.human.EntityPersonnelPalmprint;
import com.ocom.common.entity.payconfig.EntityFkParamConfig;
import com.ocom.common.entity.payconfig.EntityFkUserparamConfig;
import com.ocom.common.entity.srvpaccess.XfWhiteInfo;
import com.ocom.common.enums.MachEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.human.PersonQueryRequest;
import com.ocom.common.resp.RespMachMessage;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.RespUtilMach;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.StringUtils;
import com.ocom.payhandle.service.*;
import com.ocom.payhandle.vo.PersonnelInfoVo;
import com.ocom.payhandle.vo.RePersonnelInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class XFMachineController {

    @Autowired
    private DeviceInfoService xfDeviceInfoService;

    @Autowired
    private XfWhiteInfoService xfWhiteInfoService;

    @Autowired
    private PersonnelInfoVoService personnelInfoVoService;

    @Autowired
    private FkParamConfigService fkParamConfigService;

    @Autowired
    private XfMerchantInfoService xfMerchantInfoService;

    @Autowired
    private FkUserparamConfigService fkUserparamConfigService;

    @Autowired
    private XfDeviceInfoService xfDeviceInfoServices;

    @Autowired
    private XfPointInfoService xfPointInfoService;

    @Autowired
    private XfSignNoService xfSignNoService;

    /*
     * @Description: 获取白名单
     * @Author: lu
     * @Date: 2022/6/28 15:38
     * @param jsonObject:
     * @return: com.ocom.common.resp.RespMachMessage
     **/
    @PostMapping(value = PayHandleUrl.URL_getXFWhiteList)
    public RespMachMessage changeWhiteListState(@RequestBody JSONObject jsonObject){
        if (jsonObject.getLong("companyID") ==null || StringUtils.isEmpty(jsonObject.getString("machNo"))
                || jsonObject.getInteger("listFormat")==null){
            return RespUtilMach.error(MachEnum.REQUIRED_NULL);
        }
        int listLimit=10;
        if (jsonObject.getInteger("listLimit")!=null){
            listLimit=jsonObject.getInteger("listLimit");
            if(listLimit>50){
                listLimit=50;
            }
        }
        try {
            EntityXfDeviceInfo xfDeviceInfo=xfDeviceInfoService.selectDeviceInfolist(jsonObject.getLong("companyID"),jsonObject.getString("machNo"));
            if (xfDeviceInfo==null){
                return RespUtilMach.error(MachEnum.MACH_NOT_INFO);
            }
            Map<String,Object> map=new HashMap<>();
            List<RePersonnelInfoVo> list=null;
            //当xfDeviceInfo.getListAdd()==null 或者 -1时是全量下发
            if (xfDeviceInfo.getListAdd()==null || xfDeviceInfo.getListAdd()==-1){
                //查询白名单最大id
                XfWhiteInfo xfWhiteInfoMax=xfWhiteInfoService.selectMaxId(jsonObject.getLong("companyID"));
                //获取新增的白名单
                List<XfWhiteInfo> xfWhiteInfoList=xfWhiteInfoService.selectList(jsonObject.getLong("companyID"),listLimit,null,null);
                if (xfWhiteInfoList.size()<=0){
                    return RespUtilMach.error(MachEnum.NOT_SEARCH);
                }
                //获取数组的最大值
                int idMax=xfWhiteInfoList.stream().mapToInt(XfWhiteInfo::getId).max().getAsInt();
//                log.info("消费-最大id ，idMax 【{}】",idMax);
                list=getPersonnelInfoVoList(jsonObject.getLong("companyID"),xfWhiteInfoList,jsonObject.getInteger("listFormat"), jsonObject.getString("flag"));
                if (list==null || list.size()<=0){
                    return RespUtilMach.error(MachEnum.WHITENULL);
                }

                map.put("flag",2);
                map.put("model",0);
                //修改机器表的状态
                xfDeviceInfoService.updateXFDeviceInfo(jsonObject.getLong("companyID"),null,idMax,xfWhiteInfoMax.getId(),xfWhiteInfoMax.getId(),null,jsonObject.getString("machNo"));
            }else {
                //增量下发
                //获取新增的白名单
                List<XfWhiteInfo> xfWhiteInfoList=xfWhiteInfoService.selectList(jsonObject.getLong("companyID"),listLimit,xfDeviceInfo.getListAdd(),0);
//                log.info("消费-获取新增的白名单22 ， 【{}】",xfWhiteInfoList);
                if (xfWhiteInfoList !=null && xfWhiteInfoList.size()>0){
                    //获取数组的最大值
                    int idMax=xfWhiteInfoList.stream().mapToInt(XfWhiteInfo::getId).max().getAsInt();
//                    int idMax= xfWhiteInfoList.stream().mapToInt(XfWhiteInfo->XfWhiteInfo.getId()).max().getAsInt();
//                    int idMax= getMaxId(xfWhiteInfoList);
//                    log.info("消费-获取新增的白名单-最大id ，idMax 【{}】",idMax);
                    list=getPersonnelInfoVoList(jsonObject.getLong("companyID"),xfWhiteInfoList,jsonObject.getInteger("listFormat"), jsonObject.getString("flag"));
                    if (list==null || list.size()<=0){
                        return RespUtilMach.error(MachEnum.WHITENULL);
                    }
                    map.put("flag",2);
                    map.put("model",1);
                    //修改机器表的状态
                    xfDeviceInfoService.updateXFDeviceInfo(jsonObject.getLong("companyID"),null,idMax,null,null,null,jsonObject.getString("machNo"));
                }else {
                    //删除
                    List<XfWhiteInfo> xfWhiteDelelteList=xfWhiteInfoService.selectList(jsonObject.getLong("companyID"),listLimit,xfDeviceInfo.getListDelete(),1);
//                    log.info("消费-获取删除的白名单 ， 【{}】",xfWhiteDelelteList);
                    if (xfWhiteDelelteList !=null && xfWhiteDelelteList.size()>0){
                        //获取数组的最大值
                        int idMax=xfWhiteDelelteList.stream().mapToInt(XfWhiteInfo::getId).max().getAsInt();
//                        int idMax= xfWhiteInfoList.stream().mapToInt(XfWhiteInfo->XfWhiteInfo.getId()).max().getAsInt();
//                        int idMax= getMaxId(xfWhiteDelelteList);
//                        log.info("消费-获取删除的白名单-最大id ，idMax 【{}】",idMax);
                        list=getPersonnelInfoVoList(jsonObject.getLong("companyID"),xfWhiteDelelteList,jsonObject.getInteger("listFormat"), jsonObject.getString("flag"));
                        if (list==null || list.size()<=0){
                            return RespUtilMach.error(MachEnum.WHITENULL);
                        }
                        map.put("flag",1);
                        map.put("model",1);
                        //修改机器表的状态
                        xfDeviceInfoService.updateXFDeviceInfo(jsonObject.getLong("companyID"),null,null,null,idMax,null,jsonObject.getString("machNo"));
                    }else {
                        xfDeviceInfoService.updateXFDeviceInfo(jsonObject.getLong("companyID"),null,null,null,null,2,jsonObject.getString("machNo"));
                    }
                }
            }
            map.put("list",list);
            return RespUtilMach.success(map);
        }catch (Exception e){
            log.info("消费的下传白名单出异常， 参数【{}】e【{}】" ,jsonObject, e);
            return RespUtilMach.error(MachEnum.ERR);
        }

    }

    /*
     * @Description:白名单通知
     * @Author: lu
     * @Date: 2022/6/28 15:39
     * @param jsonObject:
     * @return: com.ocom.common.resp.RespMachMessage
     **/
    @PostMapping(value = PayHandleUrl.URL_getXFWhiteListResult)
    public RespMachMessage getXFWhiteListResult(@RequestBody JSONObject jsonObject){
        try {
            if (jsonObject.getInteger("result")==1){
                return RespUtilMach.error(MachEnum.FAIL);
            }
            EntityXfDeviceInfo xfDeviceInfo=xfDeviceInfoService.selectDeviceInfolist(jsonObject.getLong("companyID"),jsonObject.getString("machNo"));
            if (xfDeviceInfo==null){
                return RespUtilMach.error(MachEnum.MACH_NOT_INFO);
            }
            Map<String,Object> map=new HashMap<>();
            if (xfDeviceInfo.getListState()==1){
                xfDeviceInfoService.updateXFDeviceInfo(jsonObject.getLong("companyID"),xfDeviceInfo.getListAddTab(),null,xfDeviceInfo.getListDeleteTab(),null,null,jsonObject.getString("machNo"));
                map.put("flag",1);
                return RespUtilMach.success(map);
            }else if (xfDeviceInfo.getListState()==2){
                XfWhiteInfo xfWhiteInfo=xfWhiteInfoService.selectXFWhite(jsonObject.getLong("companyID"),xfDeviceInfo);
                if (xfWhiteInfo==null){
                    xfDeviceInfoService.updateXFDeviceInfo(jsonObject.getLong("companyID"),null,null,null,null,3,jsonObject.getString("machNo"));
                    map.put("flag",0);
                    return RespUtilMach.success(map);
                }else {
                    map.put("flag",1);
                    return RespUtilMach.success(map);
                }
            }else {
                map.put("flag",0);
                return RespUtilMach.success(map);
            }
        }catch (Exception e){
            log.info("消费的白名单更新出异常，e:"+e);
            return RespUtilMach.error(MachEnum.ERR);
        }
    }

    private List<RePersonnelInfoVo> getPersonnelInfoVoList(long companyId, List<XfWhiteInfo> list, int listFormat, String flag){
        List<Integer> courseIds=  list.stream().map(XfWhiteInfo::getPId).collect(Collectors.toList());
//        log.info("获取Id ss : XfWhiteInfo 【{}】 ：" ,list);
//        log.info("获取Id ss : 【{}】 ：" ,courseIds);
        //获取卡名单
        if (listFormat==1){
            List<PersonnelInfoVo> personnelInfoVos=personnelInfoVoService.selectCard(companyId,courseIds);
            if (personnelInfoVos !=null && personnelInfoVos.size()>0){
                List<RePersonnelInfoVo> rePersonnelInfoVos=new ArrayList<>();
                for (PersonnelInfoVo personnelInfoVo : personnelInfoVos) {
                    RePersonnelInfoVo rePersonnelInfoVo=new RePersonnelInfoVo();
                    rePersonnelInfoVo.setCard_No(personnelInfoVo.getCardNo());
                }
                return rePersonnelInfoVos;
            }
        }else if (listFormat==2){
            //人员白名单
            List<PersonnelInfoVo> personnelInfoVos=personnelInfoVoService.selectPersonnelInfoCard(companyId,courseIds);
            if (personnelInfoVos !=null && personnelInfoVos.size()>0){
                List<RePersonnelInfoVo> rePersonnelInfoVos=new ArrayList<>();
                for (PersonnelInfoVo personnelInfoVo : personnelInfoVos) {
                    RePersonnelInfoVo rePersonnelInfoVo=new RePersonnelInfoVo();
                    rePersonnelInfoVo.setCard_No(personnelInfoVo.getCardNo());
                    rePersonnelInfoVo.setCardType(personnelInfoVo.getAccTypeId());
                    rePersonnelInfoVo.setP_ID(personnelInfoVo.getPId());
                    rePersonnelInfoVo.setP_Name(personnelInfoVo.getPName());
                    rePersonnelInfoVo.setCardstate(personnelInfoVo.getCardState());
                    rePersonnelInfoVos.add(rePersonnelInfoVo);
                }
                return rePersonnelInfoVos;
            }
        }else if (listFormat==3){
            //人员白名单（带照片）
            List<PersonnelInfoVo> personnelInfoVos=personnelInfoVoService.selectPersonnelInfoUrl(companyId,courseIds, flag);
            if (personnelInfoVos !=null && personnelInfoVos.size()>0){
                List<RePersonnelInfoVo> rePersonnelInfoVos=new ArrayList<>();
                for (PersonnelInfoVo personnelInfoVo : personnelInfoVos) {
                    RePersonnelInfoVo rePersonnelInfoVo=new RePersonnelInfoVo();
                    rePersonnelInfoVo.setCard_No(personnelInfoVo.getCardNo());
                    rePersonnelInfoVo.setCardType(personnelInfoVo.getAccTypeId());
                    rePersonnelInfoVo.setP_ID(personnelInfoVo.getPId());
                    if (CommonUtil.isNull(personnelInfoVo.getPPhotoUrl2())){
                        rePersonnelInfoVo.setPicture(personnelInfoVo.getPPhotoUrl());
                        rePersonnelInfoVo.setFeatureValues(personnelInfoVo.getFeature());
                    }else {
                        rePersonnelInfoVo.setPicture(personnelInfoVo.getPPhotoUrl2());
                        rePersonnelInfoVo.setFeatureValues(personnelInfoVo.getFeature2());
                    }
//                    log.info("身份证【{}】",personnelInfoVo.getPPhotoUrl2());
                    rePersonnelInfoVo.setP_Name(personnelInfoVo.getPName());
                    rePersonnelInfoVo.setCardstate(personnelInfoVo.getCardState());
                    rePersonnelInfoVos.add(rePersonnelInfoVo);
                }
                return rePersonnelInfoVos;
            }
        }else if (listFormat==4){
            //人员白名单
            List<PersonnelInfoVo> personnelInfoVos=personnelInfoVoService.selectPersonnelInfoCard(companyId,courseIds);
            if (personnelInfoVos !=null && personnelInfoVos.size()>0){

                List<EntityXfSignNo>  entityXfSignNoList = xfSignNoService.getSignList(companyId,28,courseIds);

                Map<Long, String>  signNos= new HashMap<>();
                if(entityXfSignNoList!=null && entityXfSignNoList.size()>0){
                    //list转map
                    signNos = entityXfSignNoList.stream().collect(Collectors.toMap(e -> e.getPId(),
                            e -> e.getFaceOpenId(),
                            (e1, e2) -> e1));
                }

                List<RePersonnelInfoVo> rePersonnelInfoVos=new ArrayList<>();
                for (PersonnelInfoVo personnelInfoVo : personnelInfoVos) {
                    RePersonnelInfoVo rePersonnelInfoVo=new RePersonnelInfoVo();
                    rePersonnelInfoVo.setCard_No(personnelInfoVo.getCardNo());
                    rePersonnelInfoVo.setCardType(personnelInfoVo.getAccTypeId());
                    rePersonnelInfoVo.setP_ID(personnelInfoVo.getPId());
                    rePersonnelInfoVo.setP_Name(personnelInfoVo.getPName());
                    rePersonnelInfoVo.setCardstate(personnelInfoVo.getCardState());
                    if(signNos.containsKey(personnelInfoVo.getPId().longValue())){
                        rePersonnelInfoVo.setUId(signNos.get(personnelInfoVo.getPId().longValue()));
                    }else{
                        rePersonnelInfoVo.setUId("");
                    }
                    rePersonnelInfoVos.add(rePersonnelInfoVo);
                }
                return rePersonnelInfoVos;
            }
        }else if (listFormat==5){
            //人员白名单（掌纹带照片）
            List<PersonnelInfoVo> personnelInfoVos=personnelInfoVoService.selectPersonnelInfoUrl(companyId,courseIds, flag);

            if (personnelInfoVos !=null && personnelInfoVos.size()>0){

                PersonQueryRequest personQueryRequest = new PersonQueryRequest();
                personQueryRequest.setComId(companyId);
                personQueryRequest.setPIds(courseIds);

                List<EntityPersonnelPalmprint> palmprints = personnelInfoVoService.getPalmByPIds(companyId, courseIds);

                Map<Integer, List<EntityPersonnelPalmprint>> palmMap = palmprints.stream().collect(Collectors.groupingBy(e -> e.getPId()));

                List<RePersonnelInfoVo> rePersonnelInfoVos=new ArrayList<>();
                for (PersonnelInfoVo personnelInfoVo : personnelInfoVos) {
                    RePersonnelInfoVo rePersonnelInfoVo=new RePersonnelInfoVo();
                    rePersonnelInfoVo.setCard_No(personnelInfoVo.getCardNo());
                    rePersonnelInfoVo.setCardType(personnelInfoVo.getAccTypeId());
                    rePersonnelInfoVo.setP_ID(personnelInfoVo.getPId());
                    if (CommonUtil.isNull(personnelInfoVo.getPPhotoUrl2())){
                        rePersonnelInfoVo.setPicture(personnelInfoVo.getPPhotoUrl());
                        rePersonnelInfoVo.setFeatureValues(personnelInfoVo.getFeature());
                    }else {
                        rePersonnelInfoVo.setPicture(personnelInfoVo.getPPhotoUrl2());
                        rePersonnelInfoVo.setFeatureValues(personnelInfoVo.getFeature2());
                    }
                    rePersonnelInfoVo.setP_Name(personnelInfoVo.getPName());
                    rePersonnelInfoVo.setCardstate(personnelInfoVo.getCardState());

                    rePersonnelInfoVo.setPalmList(palmMap.get(personnelInfoVo.getPId()));

                    rePersonnelInfoVos.add(rePersonnelInfoVo);
                }
                return rePersonnelInfoVos;
            }

        }
        return null;
    }

    /*
     * @Description: 获取风控参数（getMachFk）
     * @Author: lu
     * @Date: 2022/7/14 15:32
     * @param jsonObject:
     * @return: com.ocom.common.resp.RespMachMessage
     **/
    @PostMapping(value = PayHandleUrl.URL_getMachFk)
    public RespMachMessage getMachFk(Long companyId,String machNo){
        if (companyId==null || machNo==null){
            return RespUtilMach.error(MachEnum.REQUIRED_NULL);
        }
        try {
            Map<String,Object> map=new HashMap<>();
            int parentMerchId=0;
            //1.查询设备消费点id；
            EntityXfDeviceInfo deviceInfoVo=xfDeviceInfoServices.selectPointId(companyId,machNo);

            Integer companyTotal=fkParamConfigService.selectFKParamConfigCount(companyId);
            //7.如果第4点未能查找到风控信息，则默认返回赋值，jobId =fk_jobid, offlineCanPay=0；
            if(companyTotal==null || companyTotal==0){
                map.put("jobId",deviceInfoVo.getFkparaJobid());
                //当前机具是否允许发起⽀支付及允许消费的⽅方式
                map.put("can_pay",0);
                return RespUtilMach.success(map);
            }

            //收费点时0，机器挂在单位下
            EntityFkParamConfig entityFkParamConfig=null;
            if (deviceInfoVo.getPointId()==null || deviceInfoVo.getPointId()==0){
                entityFkParamConfig=fkParamConfigService.selectFKParamConfigCompanyId(companyId);
            }else {
                entityFkParamConfig=fkParamConfigService.selectFKParamConfigList(companyId,parentMerchId);
            }
            EntityXfPointInfo entityXfPointInfo=xfPointInfoService.getTollPointInfo(companyId,deviceInfoVo.getPointId());
            List<Map<String,Object>> list=new ArrayList<>();
            if (entityXfPointInfo==null){
//                parentMerchId=entityFkParamConfig.getMerchId();
                parentMerchId=0;
            }else {
                parentMerchId=entityXfPointInfo.getMerchId();
            }

            List<EntityFkUserparamConfig> entityFkUserparamConfig=fkUserparamConfigService.selectFkUserparamConfigList(companyId,parentMerchId);
            if (entityFkParamConfig == null) {
                //4.如果未获取到该商户的风控参数，则查找该商户的上级商户风控参数（直到查找到该单位风控参数）。
                //查找上级商户id:
                for (int i = 0; i < 100; i++) {
                    EntityXfMerchantInfo entityXfMerchantInfo=xfMerchantInfoService.selectParentMerchId(companyId,parentMerchId);
                    if (entityXfMerchantInfo!=null){
                        if (entityXfMerchantInfo.getParentMerchId()>0){
                            parentMerchId=entityXfMerchantInfo.getParentMerchId();
                            EntityFkParamConfig entityFkParamConfigs=fkParamConfigService.selectFKParamConfigList(companyId,parentMerchId);
                            if (entityFkParamConfigs != null) {
                                map.put("jobId",deviceInfoVo.getFkparaJobid());
                                //当前机具是否允许发起⽀支付及允许消费的⽅方式
                                map.put("can_pay",3);
                                map.put("offline_amount",entityFkParamConfigs.getAllamountOffline());
                                map.put("offline_order_num",entityFkParamConfigs.getAlltimesOffline());
                                map.put("offline_time",entityFkParamConfigs.getMaxtimeOffline());
                                if (StringUtils.isNotEmpty(deviceInfoVo.getCodePicUrl())){
                                    map.put("offlinecodeurl",deviceInfoVo.getCodePicUrl());
                                }else {
                                    map.put("offlinecodeurl",entityFkParamConfigs.getOfflineUrl());
                                }
                                map.put("state_flag",1);
                                map.put("refund_mode",entityFkParamConfigs.getRefundMode());
                                //是否允许离线消费,0 不允许，1允许
                                if (entityFkParamConfigs.getIsOffline()==0){
                                    map.put("online_mode",1);
                                }else {
                                    map.put("online_mode",2);
                                }
                                Map<String,Object> mapIdentityLimit=new HashMap<>();
                                mapIdentityLimit.put("identity",0);
                                mapIdentityLimit.put("amount",entityFkParamConfigs.getOneamountOfflin());
                                mapIdentityLimit.put("order_amount",entityFkParamConfigs.getMaxamountOfflin());
                                mapIdentityLimit.put("order_num",entityFkParamConfigs.getOnetimesOffline());
                                list.add(mapIdentityLimit);
                                if (entityFkUserparamConfig!=null && entityFkUserparamConfig.size()>0){
                                    for (EntityFkUserparamConfig fkUserparamConfig : entityFkUserparamConfig) {
                                        Map<String,Object> mapIdentityLimits=new HashMap<>();
                                        mapIdentityLimits.put("identity",fkUserparamConfig.getUserType());
                                        mapIdentityLimits.put("amount",fkUserparamConfig.getOneamountOfflin());
                                        mapIdentityLimits.put("order_amount",fkUserparamConfig.getMaxamountOfflin());
                                        mapIdentityLimits.put("order_num",fkUserparamConfig.getOnetimesOffline());
                                        list.add(mapIdentityLimits);
                                    }
                                }
                                list.sort(Comparator.comparing(o -> String.valueOf(o.get("identity"))));
                                map.put("identity_limit",list);
                                return RespUtilMach.success(map);
                            }
                        }
                    }
                }
            }else {
                map.put("jobId",deviceInfoVo.getFkparaJobid());
                //当前机具是否允许发起⽀支付及允许消费的⽅方式
                map.put("can_pay",3);
                map.put("offline_amount",entityFkParamConfig.getAllamountOffline());
                map.put("offline_order_num",entityFkParamConfig.getAlltimesOffline());
                map.put("offline_time",entityFkParamConfig.getMaxtimeOffline());
                if (StringUtils.isNotEmpty(deviceInfoVo.getCodePicUrl())){
                    map.put("offlinecodeurl",deviceInfoVo.getCodePicUrl());
                }else {
                    map.put("offlinecodeurl",entityFkParamConfig.getOfflineUrl());
                }
                map.put("refund_mode",entityFkParamConfig.getRefundMode());
                map.put("state_flag",1);
                //是否允许离线消费,0 不允许，1允许
                if (entityFkParamConfig.getIsOffline()==0){
                    map.put("online_mode",1);
                }else {
                    map.put("online_mode",2);
                }
                Map<String,Object> mapIdentityLimit=new HashMap<>();
                mapIdentityLimit.put("identity",0);
                mapIdentityLimit.put("amount",entityFkParamConfig.getOneamountOfflin());
                mapIdentityLimit.put("order_amount",entityFkParamConfig.getMaxamountOfflin());
                mapIdentityLimit.put("order_num",entityFkParamConfig.getOnetimesOffline());
                list.add(mapIdentityLimit);
                if (entityFkUserparamConfig!=null && entityFkUserparamConfig.size()>0){
                    for (EntityFkUserparamConfig fkUserparamConfig : entityFkUserparamConfig) {
                        Map<String,Object> mapIdentityLimits=new HashMap<>();
                        mapIdentityLimits.put("identity",fkUserparamConfig.getUserType());
                        mapIdentityLimits.put("amount",fkUserparamConfig.getOneamountOfflin());
                        mapIdentityLimits.put("order_amount",fkUserparamConfig.getMaxamountOfflin());
                        mapIdentityLimits.put("order_num",fkUserparamConfig.getOnetimesOffline());
                        list.add(mapIdentityLimits);
                    }
                }
                list.sort(Comparator.comparing(o -> String.valueOf(o.get("identity"))));
                map.put("identity_limit",list);
                return RespUtilMach.success(map);
            }
            map.put("online_mode",1);
            map.put("jobId",deviceInfoVo.getFkparaJobid());
            return RespUtilMach.success(map);
        }catch (Exception e){
            log.info("获取风控参数出异常，e:"+e);
            return RespUtilMach.error(MachEnum.ERR);
        }
    }


    /*
     * @Description:更改风控参数状态
     * @Author: lu
     * @Date: 2022/7/15 15:29
     * @param companyId:
     * @param machNo:
     * @param jobId:
     * @return: com.ocom.common.resp.RespMachMessage
     **/
    @PostMapping(value = PayHandleUrl.URL_changeFkState)
    public RespMachMessage changeFkState(Long companyId,String machNo,String jobId){
        if (companyId == null || machNo==null || jobId==null) {
            return RespUtilMach.error(MachEnum.REQUIRED_NULL);
        }
        try {
            Map<String,Object> map=new HashMap<>();
            if (xfDeviceInfoServices.updateJobId(companyId,machNo,jobId)){
                map.put("flag",0);
                return RespUtilMach.success(map);
            }else {
                map.put("flag",1);
                return RespUtilMach.success(map);
            }
        }catch (Exception e){
            log.info("获取风控参数出异常，e:"+e);
            return RespUtilMach.error(MachEnum.ERR);
        }
    }


    // (GetMerchInfoByMachNo)
    @PostMapping(PayHandleUrl.URLP_PayConfig_GetMerchInfoByMachNo)
    public Result GetMerchInfoByMachNo(Long comId, String machNo){
        if (comId == null ){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        return  xfMerchantInfoService.GetMerchInfoByMachNo(comId,machNo);
    }


    public Integer getMaxId(List<XfWhiteInfo> xfWhiteInfoList){
//        log.info("getMaxId  xfWhiteInfoList : 【{】】",xfWhiteInfoList);
        if(xfWhiteInfoList==null || xfWhiteInfoList.size()==0){
//            log.info("getMaxId  返回 :  【{}】】 【{}】】",0,xfWhiteInfoList);
            return  0;
        }
        Integer max = 0 ;
        for (XfWhiteInfo xfWhiteInfo:xfWhiteInfoList){
            if(xfWhiteInfo.getId().compareTo(max)>0){
                max=xfWhiteInfo.getId();
            }
        }
//        log.info("getMaxId  返回 :  【{}】】 【{}】】",max,xfWhiteInfoList);
        return max;
    }

    public static void main(String[]  args){
        List<XfWhiteInfo> xfWhiteInfoList= new ArrayList<>();

        XfWhiteInfo xfWhiteInfo1=new XfWhiteInfo();
        xfWhiteInfo1.setId(1);
        XfWhiteInfo xfWhiteInfo2=new XfWhiteInfo();
        xfWhiteInfo2.setId(2);

        xfWhiteInfoList.add(xfWhiteInfo1);
        xfWhiteInfoList.add(xfWhiteInfo2);

        int max = xfWhiteInfoList.stream().mapToInt(XfWhiteInfo->XfWhiteInfo.getId()).max().getAsInt();

//        Optional<XfWhiteInfo> optionalXfWhiteInfo= xfWhiteInfoList.stream().max(Comparator.comparingInt(XfWhiteInfo::getId));
//
//        XfWhiteInfo c = optionalXfWhiteInfo.get();
//        System.out.println(c);
    }

    @PostMapping(PayHandleUrl.URL_getComWhiteList)
    public Result getComWhiteList(@RequestParam("companyId") Long companyId, @RequestParam("personVer")Integer personVer, @RequestParam("listLimi")Integer listLimi){
        return ResultUtil.success(xfWhiteInfoService.getComWhiteList(companyId, personVer, listLimi));
    }
}
